﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.ServiceModel;
using Anagramme.Server.Service;

namespace Anagramme.Server
{
    class Program
    {        
        private delegate bool EventHandler(CtrlType sig);

        private const int VK_RETURN = 0x0D;
        private const int WM_KEYDOWN = 0x100; 

        //static EventHandler _handler;

        enum CtrlType
        {
            CTRL_C_EVENT = 0,
            CTRL_BREAK_EVENT = 1,
            CTRL_CLOSE_EVENT = 2,
            CTRL_LOGOFF_EVENT = 5,
            CTRL_SHUTDOWN_EVENT = 6
        }

        enum ShowType
        {
            SW_HIDE = 0,
            SW_SHOWNORMA = 1,
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Supprimer les objets avant la mise hors de portée"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "args")]
        static void Main(string[] args)
        {
            Console.Title = "Anagramme Server";

#if DEBUG
            setConsoleWindowVisibility(true, Console.Title);
#else
            setConsoleWindowVisibility(false, Console.Title);
#endif
            
            ServiceHost lobbyServiceHost = null;
            ServiceHost gameServiceHost = null;
            ServiceHost resultsServiceHost = null;

            try
            {
                lobbyServiceHost = new ServiceHost(typeof(LobbyService));
                gameServiceHost = new ServiceHost(typeof(GameService));
                resultsServiceHost = new ServiceHost(typeof(ResultsService));

                lobbyServiceHost.Open();
                gameServiceHost.Open();
                resultsServiceHost.Open();

                //_handler += new EventHandler(c =>
                //{
                //    ServerService.Close();
                //    return true;
                //});

                //SetConsoleCtrlHandler(_handler, true);

                Console.ReadKey();
            }
            finally 
            {
                if (lobbyServiceHost != null)
                {
                    lobbyServiceHost.Close();
                    ((IDisposable)lobbyServiceHost).Dispose();
                    lobbyServiceHost = null;
                }
                if (gameServiceHost != null)
                {
                    gameServiceHost.Close();
                    ((IDisposable)gameServiceHost).Dispose();
                    gameServiceHost = null;
                }
                if (resultsServiceHost != null)
                {
                    resultsServiceHost.Close();
                    ((IDisposable)resultsServiceHost).Dispose();
                    resultsServiceHost = null;
                }
            }
        }

        public static void setConsoleWindowVisibility(bool visible, string title)
        {

            IntPtr hWnd = NativeMethods.FindWindow(null, title);

            if (hWnd != IntPtr.Zero)
            {
                if (!visible)
                    NativeMethods.ShowWindow(hWnd, (int)ShowType.SW_HIDE);     
                else
                    NativeMethods.ShowWindow(hWnd, (int)ShowType.SW_SHOWNORMA);      
            }
        }
    }
}
